package com.tl.satoken.controller;

import com.tl.satoken.common.Result;
import com.tl.satoken.service.GameRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping({"/api/game"})
public class GameController {

    @Autowired
    private GameRoomService gameRoomService;

    @GetMapping("/match/{gameId}/count")
    public Result<Integer> getGameMatchCount(@PathVariable Integer gameId) {
        try {
            Integer count = gameRoomService.getGameMatchCount(gameId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("获取匹配人数失败: " + e.getMessage());
        }
    }

    @GetMapping("/match/{gameId}/players")
    public Result<java.util.List<Integer>> getGameMatchPlayers(@PathVariable Integer gameId) {
        try {
            java.util.List<Integer> players = gameRoomService.getGameMatchPlayers(gameId);
            return Result.success(players);
        } catch (Exception e) {
            return Result.error("获取匹配玩家列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/player/{heroId}/match")
    public Result<Boolean> isPlayerInMatch(@PathVariable Integer heroId) {
        try {
            boolean inMatch = gameRoomService.isPlayerInMatch(heroId);
            return Result.success(inMatch);
        } catch (Exception e) {
            return Result.error("检查玩家匹配状态失败: " + e.getMessage());
        }
    }

    @GetMapping("/player/{heroId}/room")
    public Result<String> getPlayerMatchRoom(@PathVariable Integer heroId) {
        try {
            String roomId = gameRoomService.getPlayerMatchRoomId(heroId);
            return Result.success(roomId);
        } catch (Exception e) {
            return Result.error("获取玩家匹配房间失败: " + e.getMessage());
        }
    }

    @GetMapping("/game/{gameId}/rooms")
    public Result<java.util.List<String>> getGameRooms(@PathVariable Integer gameId) {
        try {
            java.util.List<String> rooms = gameRoomService.getGameRooms(gameId);
            return Result.success(rooms);
        } catch (Exception e) {
            return Result.error("获取游戏房间列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/room/{roomId}/players")
    public Result<java.util.List<Integer>> getRoomPlayers(@PathVariable String roomId) {
        try {
            java.util.List<Integer> players = gameRoomService.getRoomPlayers(roomId);
            return Result.success(players);
        } catch (Exception e) {
            return Result.error("获取房间玩家列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/room/{roomId}/count")
    public Result<Integer> getRoomPlayerCount(@PathVariable String roomId) {
        try {
            Integer count = gameRoomService.getRoomPlayerCount(roomId);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("获取房间玩家数量失败: " + e.getMessage());
        }
    }

    @GetMapping("/room/{roomId}/full")
    public Result<Boolean> isRoomFull(@PathVariable String roomId) {
        try {
            boolean isFull = gameRoomService.isRoomFull(roomId);
            return Result.success(isFull);
        } catch (Exception e) {
            return Result.error("检查房间是否满员失败: " + e.getMessage());
        }
    }
}

